package com.xiaoyang.ssyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoyang.ssyx.acl.mapper.AdminMapper;
import com.xiaoyang.ssyx.acl.service.AdminService;
import com.xiaoyang.ssyx.common.utils.MD5;
import com.xiaoyang.ssyx.model.acl.Admin;
import com.xiaoyang.ssyx.vo.acl.AdminQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @AUTHOR XiaoYang
 * @DATE 2023/6/13-19:32
 * @DESCRIPTION TODO
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Override
    public IPage<Admin> selectPageUser(Page<Admin> pageParam, AdminQueryVo adminQueryVo) {
        String username = adminQueryVo.getUsername();
        String name = adminQueryVo.getName();
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(username)){
            wrapper.eq(Admin::getUsername,username);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.like(Admin::getName,name);
        }
        IPage<Admin> adminPage = baseMapper.selectPage(pageParam, wrapper);
        return adminPage;
    }

    /*
    添加用户
     */
    @Override
    public boolean saveOfMd5(Admin admin) {
        String password = admin.getPassword();
        String passwordMD5 = MD5.encrypt(password);
        admin.setPassword(passwordMD5);
        boolean isSuccess = save(admin);
        return isSuccess;
    }
}
